package cc.magicjson.task;

import cc.magicjson.entity.TaskConfig;
import cc.magicjson.repository.TaskConfigRepository;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class MyScheduledTask {

	private final TaskConfigRepository taskConfigRepository;

	public MyScheduledTask(TaskConfigRepository taskConfigRepository) {
		this.taskConfigRepository = taskConfigRepository;
	}

	// 定时任务的实际执行逻辑
	public void performTask() {
		Optional<TaskConfig> taskConfigOpt = taskConfigRepository.findByTaskName("myTask");

		if (taskConfigOpt.isPresent()) {
			TaskConfig config = taskConfigOpt.get();
			System.out.println("从数据库获取的配置: " + config.getTaskName() + ", 启用状态: " + config.isEnabled());

			if (config.isEnabled()) {
				System.out.println("定时任务执行中...");
			} else {
				System.out.println("定时任务未启用...");
			}
		} else {
			System.out.println("未找到任务配置");
		}
	}

}
